package cn.edu.scau.cmi.suyaoxing.client;

import cn.edu.scau.cmi.suyaoxing.hibernate.dao.*;
import cn.edu.scau.cmi.suyaoxing.hibernate.domain.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class HibernateClient extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	int choice;
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	ITeacherDAO tchDAO = new TeacherDAO();
	IStudentDAO stuDAO = new StudentDAO();
	private String[] type = { "学生", "教师" };

	public void start(Stage primaryStage) {
		GridPane gridpane = new GridPane();
		gridpane.setAlignment(Pos.CENTER);
		gridpane.setHgap(1);
		gridpane.setVgap(1);

		Label lb1 = new Label("StudentID:");
		Label lb2 = new Label("StudentName:");
		Label lb3 = new Label("TeacherID:");
		Label lb4 = new Label("TeacherName:");
		Button bt1 = new Button("添加");
		Button bt2 = new Button("删除");
		Button bt3 = new Button("修改");
		Button bt4 = new Button("查找");
		Button bt5 = new Button("列表");
		TextField tf1 = new TextField();
		TextField tf2 = new TextField();
		TextField tf3 = new TextField();
		TextField tf4 = new TextField();
		TextArea ta = new TextArea();
		ComboBox<String> cbo = new ComboBox<>();
		ObservableList<String> items = FXCollections.observableArrayList(type);
		cbo.getItems().addAll(items);

		gridpane.add(bt1, 2, 0,1,1);
		gridpane.add(bt2, 3, 0,1,1);
		gridpane.add(bt3, 4, 0,1,1);
		gridpane.add(bt4, 5, 0,1,1);
		gridpane.add(bt5, 6, 0,1,1);
		gridpane.add(cbo, 7, 0,1,1);

		gridpane.add(lb1, 0, 2,2,1);
		gridpane.add(tf1, 2, 2,6,1);
		gridpane.add(lb2, 0, 3,2,1);
		gridpane.add(tf2, 2, 3,6,1);
		gridpane.add(lb3, 0, 4,2,1);
		gridpane.add(tf3, 2, 4,6,1);
		gridpane.add(lb4, 0, 5,2,1);
		gridpane.add(tf4, 2, 5,6,1);
		gridpane.add(ta, 2, 6,6,1);
        //添加
		bt1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				choice = items.indexOf(cbo.getValue());
				switch (choice) {
				case 0:
					if ((stuDAO.get(Long.parseLong(tf1.getText())) != null)
							|| (tchDAO.get(Long.parseLong(tf3.getText())) == null)) {
						ta.setText("学生ID已存在或导师ID不存在");
					} else {
						Student student = new Student();
						//Teacher teacher = tchDAO.get(Long.parseLong(tf3.getText()));
						Teacher teacher=new Teacher();
						teacher.setId(Long.parseLong(tf3.getText()));
						teacher.setName(tf4.getText());
						student.setId(Long.parseLong(tf1.getText()));
						student.setName(tf2.getText());
						student.setTeacher(teacher);
						stuDAO.save(student);
						ta.setText("已添加学生 " + df.format(new Date()));
						ta.appendText('\n'+student.toString());
						tf1.clear();
						tf2.clear();
						tf3.clear();
						tf4.clear();
					}
					break;
				case 1:
					if (tchDAO.get(Long.parseLong(tf3.getText())) != null) {
						ta.setText("导师ID已存在");
					} else {
						Teacher teacher = new Teacher();
						teacher.setId(Long.parseLong(tf3.getText()));
						teacher.setName(tf4.getText());
						tchDAO.save(teacher);
						ta.setText("已添加导师 " + df.format(new Date()));
						ta.appendText('\n'+teacher.toString());
						tf1.clear();
						tf2.clear();
						tf3.clear();
						tf4.clear();
					}
					break;
					default:ta.setText("请选择操作对象类型");
				}
			}
		});
		//删除
		bt2.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				choice = items.indexOf(cbo.getValue());
				switch (choice) {
				case 0:
					Student student=stuDAO.get(Long.parseLong(tf1.getText()));
					if(student==null) {
						ta.setText("指定ID的学生不存在");
					}
					else {
						stuDAO.delete(student);
						ta.setText("已删除学生 " + df.format(new Date()));
						ta.appendText('\n'+student.toString());
						tf1.clear();
						tf2.clear();
						tf3.clear();
						tf4.clear();
					}
					
					break;
				case 1:
					Teacher teacher=tchDAO.get(Long.parseLong(tf3.getText()));
					if(teacher==null) {
						ta.setText("指定ID的导师不存在");
					}
					else {
						tchDAO.delete(teacher);
						ta.setText("已删除导师 " + df.format(new Date()));
						ta.appendText('\n'+teacher.toString());
						tf1.clear();
						tf2.clear();
						tf3.clear();
						tf4.clear();
					}
					break;
				default:ta.setText("请选择操作对象类型");
				}
			}
		});
		//修改
		bt3.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				choice = items.indexOf(cbo.getValue());
				switch (choice) {
				case 0:
					Student student=stuDAO.get(Long.parseLong(tf1.getText()));
					if(student==null) {
						ta.setText("指定ID的学生不存在");
					}
					else {
						ta.setText("修改前:"+'\n'+student.toString()+'\n');
						Teacher teacher=student.getTeacher();
						student.setId(Long.parseLong(tf1.getText()));
						student.setName(tf2.getText());
						teacher.setId(Long.parseLong(tf3.getText()));
						teacher.setName(tf4.getText());
						student.setTeacher(teacher);
						stuDAO.update(student);
						ta.appendText("已更新学生信息 " + df.format(new Date()));
						ta.appendText('\n'+"修改后:"+'\n'+student.toString());
						tf1.clear();
						tf2.clear();
						tf3.clear();
						tf4.clear();
					}
					break;
				case 1:
					Teacher teacher=tchDAO.get(Long.parseLong(tf3.getText()));
					if(teacher==null) {
						ta.setText("指定ID的导师不存在");
					}
					else {
						ta.setText("修改前:"+'\n'+teacher.toString()+'\n');
						teacher.setId(Long.parseLong(tf3.getText()));
						teacher.setName(tf4.getText());
						tchDAO.update(teacher);
						ta.appendText("已更新导师信息 " + df.format(new Date()));
						ta.appendText("修改后:"+'\n'+teacher.toString());
						tf1.clear();
						tf2.clear();
						tf3.clear();
						tf4.clear();
					}
					break;
				default:ta.setText("请选择操作对象类型");
				}
			}
		});
		//查找
		bt4.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				choice = items.indexOf(cbo.getValue());
				switch (choice) {
				case 0:
					Student student=stuDAO.get(Long.parseLong(tf1.getText()));
					if(student==null) {
						ta.setText("指定ID的学生不存在");
					}
					else {
						tf1.setText(String.valueOf(student.getId()));
						tf2.setText(student.getName());
						tf3.setText(String.valueOf(student.getTeacher().getId()));
						tf4.setText(student.getTeacher().getName());
						ta.setText("查询结果："+'\n'+student.toString());
					}
					break;
				case 1:
					Teacher teacher=tchDAO.get(Long.parseLong(tf3.getText()));
					if(teacher==null) {
						ta.setText("指定ID的导师不存在");
					}
					else {
						tf1.clear();
						tf2.clear();
						tf3.setText(String.valueOf(teacher.getId()));
						tf4.setText(teacher.getName());
						ta.setText("查询结果："+'\n'+teacher.toString());
					}
					break;
				default:ta.setText("请选择操作对象类型");
				}
			}
		});
		//列表
		bt5.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				choice = items.indexOf(cbo.getValue());
				switch (choice) {
				case 0:
					List<Student> stuList=stuDAO.list();
					if(stuList.size()==0) {
						ta.setText("No Data in Table \"Student\"");
					}
					else {
						ta.setText("学生信息列表:"+'\n');
						for(Student temp:stuList){
							ta.appendText(temp.toString()+'\n');
						}
					}
					break;
				case 1:
					List<Teacher> tchList=tchDAO.list();
					if(tchList.size()==0) {
						ta.setText("No Data in Table \"Teacher\"");
					}
					else {
						ta.setText("导师信息列表:"+'\n');
						for(Teacher temp:tchList){
							ta.appendText(temp.toString()+'\n');
						}
					}
					break;
				default:ta.setText("请选择操作对象类型");
				}
			}
		});
		Scene scene = new Scene(gridpane, 1100, 500);
		primaryStage.setTitle("Hibernate框架");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}
